/**
 * @program: yygh_parent
 * @ClassName DepartmentController
 * @description:
 * @author: zyw
 * @create: 2023-05-25 09:16
 * @Version 1.0
 **/
package com.atguigu.yygh.hosp.controller.api;

import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.util.MD5;
import com.atguigu.yygh.hosp.bean.Result;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.utils.HttpRequestHelper;
import com.atguigu.yygh.model.hosp.Department;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Api("科室信息管理")
@RestController
@RequestMapping("/api/hosp")
public class ApiDepartmentController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private DepartmentService departmentService;

    @ApiOperation(value = "上传科室信息")
    @PostMapping("/saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());
        //参数校验
        String sign = (String) map.get("sign");
        String hoscode = (String) map.get("hoscode");
        //查询数据库中的sign
        String signKey = hospitalService.getSignKeyWithHoscode(hoscode);
        //进行md5加密对比
        String signKeyMD5 = MD5.encrypt(signKey);
        if (!sign.equals(signKeyMD5)) {
            throw new YyghException(20001, "校验失败");
        }
        departmentService.saveDepartment(map);
        return Result.ok();
    }

    @PostMapping("/department/list")
    @ApiOperation(value = "展示科室信息")
    public Result<Page> getDepartmentList(HttpServletRequest request) {
        Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());
        Page<Department> page = departmentService.getDepartmentPage(map);
        return Result.ok(page);
    }

    @PostMapping("/department/remove")
    @ApiOperation(value = "删除科室信息")
    public Result remove(HttpServletRequest request) {
        Map<String, Object> map = HttpRequestHelper.switchMap(request.getParameterMap());
        //参数校验
        String sign = (String) map.get("sign");
        String hoscode = (String) map.get("hoscode");
        //查询数据库中的sign
        String signKey = hospitalService.getSignKeyWithHoscode(hoscode);
        //进行md5加密对比
        String signKeyMD5 = MD5.encrypt(signKey);
        if (!sign.equals(signKeyMD5)) {
            throw new YyghException(20001, "校验失败");
        }
        departmentService.remove(map);
        return Result.ok();
    }

}
 
